<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2016/9/8
  Time: 15:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="s" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <jsp:include page="../../head.jsp"/>
    <script type="text/javascript" src="<s:url value="/js/multiDeclarationPayment.js"/>"></script>
    <title>Title</title>
    <script type="text/javascript">
        var layerIndex;
        var formSetting = {
            title: "查询信息",
            module: 'query',
            columns: [
                {
                    name: "declaration_type",
                    label: "进出口类型",
                    type: "dropdown",
                    data: [["进口", "I"], ["出口", "E"]],
                    op: "eq",
                    minLength: 0
                }, {
                    name: "contract_no",
                    label: "合同协议号"
                }, {
                    name: "businessId",
                    label: "业务编号"
                }, {
                    name: "container_no",
                    label: "集装箱号"
                }, {
                    name: "company_co",
                    label: "收发货人",
                    type: "dropdown",
                    dropdownType: "ajax",
                    url: "../order-dropdown/company",
                    op: "eq",
                    minLength: 1,
                    keyWordName: "key",
                    success: function (data) {
                        data = data.results;
                        var array = new Array();
                        $.each(data, function (i, n) {
                            array.push([n["text"], n["value"]]);
                        });
                        return array;
                    }
                }, {
                    name: "bl_no",
                    label: "提运单号"
                }, {
                    name: "supCompany.name",
                    label: "业务供应商"
                }, {
                    name: "creator",
                    label: "下单人"
                }],
            buttons: {
                custom: [{
                    id: "payBtn",
                    value: "支付",
                    click: function () {
                        $("#declarationsDataGrid").multiDeclarationPayment();
                    }
                }]
            },
            grid: {
                id: "declarationsDataGrid",
                title: "查询结果",
                url: "findUnpaidList",
                multiselect: true,
                colModel: [{
                    index: "ID",
                    name: "id",
                    hidden: true
                }, {
                    index: "state",
                    name: "state",
                    hidden: true
                },{
                    index: "ENTER_PAGE",
                    name: "enter_page",
                    hidden: true
                }, {
                    name: "businessId",
                    label: "业务编号",
                    formatter: function (cellvalue, options, rowObject) {
                        return "<a href=\"javascript:;\" onclick=\"javascript:doEditSingle('"
                            + rowObject.id + "','"
                            + rowObject.state + "','"
                            + rowObject.enter_page + "');\">" + cellvalue
                            + "</a>";
                    }
                }, {
                    name: "container_no",
                    label: "集装箱号"
                }, {
                    name: "contract_no",
                    label: "合同协议号"
                }, {
                    name: "bl_no",
                    label: "提运单号"
                }, {
                    name: "declaration_type",
                    label: "进出口类型",
                    formatter: function (cellvalue, options, rowObject) {
                        switch (cellvalue) {
                            case "E":
                                return "出口";
                                break;
                            case "I":
                                return "进口";
                                break;
                            default:
                                return "";
                        }
                    }
                }, {
                    name: "company_name",
                    label: "收发货人"
                }, {
                    name: "supCompany.name",
                    label: "业务供应商"
                }, {
                    name: "secondSupId",
                    label: "二级供应商"
                },{
                    name: "ctime",
                    label: "保存时间"/*,
                     formatter: 'date',
                     formatoptions: {
                     srcformat: 'Y-m-d H:i:s',
                     newformat: 'Y-m-d H:i:s'
                     }*/
                }, {
                    name: "creator",
                    label: "下单人"
                }, {
                    label: "操作",
                    sortable: false,
                    buttons: [{
                        key: "id1",
                        value: "修改",
                        hide: function (rowdata) {
                            return rowdata.state !== "DS00";
                        },
                        click: function (gridid, rowId, rowdata) {
                            doEditSingle(rowId, rowdata.state,rowdata.enter_page);
                        }
                    }, {
                        key: "id2",
                        value: "删除",
                        click: function (gridid, rowId, rowdata) {
                            layerIndex = layer.confirm('确定要删除该记录吗？', {
                                btn: ['确定', '取消'] //按钮
                            }, function () {
                                doDel(rowId);
                            }, function () {
                                layer.close(layerIndex);
                            });
                            /* doDel(rowId);*/
                        }
                    }],
                    formatter: $.jqGrid.custom.formatter.button
                }],
                sortname: "businessId",
                sortorder: "desc",
                jsonReader: {},
                ondblClickRow: function (rowid, iRow, iCol, e) {
                    if (rowid) {
                        var rowData = $("#declarationsDataGrid").gridMethods().getRow(rowid);
                        doEditSingle(rowid, rowData.state);
                        <%--location.href = "<s:url value="/order/detail"/>?id=" + rowid;--%>
//                        doEdit(rowid, rowData.declaration_type);
                    }
                },
                gridComplete: function () {
                    var datas = $("#declarationsDataGrid").gridMethods().getAllRowData();
                    $.each(datas, function (i, n) {
                        if (n.state == 'DS01') {
                            $("tr#" + n.id).css('color', "dodgerblue");
                            $("tr#" + n.id + " a").css('color', "dodgerblue");
                        }
                    });
                }
            }
        };
        $(document).ready(function () {
            $("#declarationsDataTable").mrdForm(formSetting);
        });

        function doEdit(id, state) {
            if (!id || !state) {
                alert("报关单数据加载可能存在错误，未找到业务id或者业务状态！");
                return;
            } else if (state == "DS00") {
                location.href = "<s:url value="/order/update?id="/>" + id;
            } else if (state == "DS01") {
                location.href = "<s:url value="/order/detail?id="/>" + id;
            } else {
                alert("不能修改非草稿状态的报关单!");
            }

        }

        function doEditSingle(id, state ,pageType) {
            ajaxLoader.ajaxPost('../datalock/tryLock?opaId=VDBS&bidId='+id, {}, function(data){
                if(!data) {
                    jw.alert(data);
                }
                if("200"==data.errcode) {
                    if (!id || !state) {
                        alert("报关单数据加载可能存在错误，未找到业务id或者业务状态！");
                        return;
                    }  if (pageType){
                        if (state == "DS00"&&pageType == "COMPLETE5") {
                            location.href = "<s:url value="/order/update?id="/>" + id;
                        } else if (state == "DS01"&&pageType == "COMPLETE5") {
                            location.href = "<s:url value="/order/detail?id="/>" + id;
                        } else if (state == "DS00"&&pageType == "CROPPED5") {
                            location.href = "<s:url value="/business/agent/order/update?id="/>" + id;
                        } else if (state == "DS01"&&pageType == "CROPPED5") {
                            location.href = "<s:url value="/business/agent/order/detail?id="/>" + id;
                        }
                    }else{
                        if (state == "DS00") {
                            location.href = "<s:url value="/order/update?id="/>" + id;
                        } else if (state == "DS01") {
                            location.href = "<s:url value="/order/detail?id="/>" + id;
                        }else {
                            alert("不能修改非草稿状态的报关单!");
                        }
                    }
                }
                else {
                    jw.alert(data.errmsg);
                }
            });

        }

        function doDel(id) {
            $.ajax({
                       url: "./deleteUnpaidDeclaration",
                       type: "POST",
                       data: "tdateaa=" + timeNum() + "&id=" + id,
                       success: function (data) {
                           layer.close(layerIndex);
                           data = $.parseJSON(data.msg);
                           alert(data.msg);
                           if (data.state == "1") {
                               $("#declarationsDataTable").mrdForm("refreshGrid");
                           }
                       }
                   });
        }

        $.fn.gridMethods = function () {
            var $myGrid = this;
            return {
                getIds: function () {
                    return $myGrid.jqGrid('getGridParam', 'selarrrow');
                },
                getId: function () {
                    return $myGrid.jqGrid("getGridParam", "selrow")
                },
                getRow: function (id) {
                    return $myGrid.jqGrid("getRowData", id);
                },
                getSelectRow: function () {
                    return this.getRow(this.getId());
                },
                getSelectRows: function () {
                    var idArr = this.getIds();
                    var rowArr = [];
                    if (idArr.length > 0) {
                        for (var i = 0; i < idArr.length; i++) {
                            var id = idArr[i];
                            rowArr.push(this.getRow(id));
                        }
                    }
                    return rowArr;
                },
                getAllRowData: function () {
                    return $myGrid.jqGrid('getRowData');
                }
            };
        };

        /**
         * 选择报关单，点击提交的逻辑
         */
        function submitBtnClickFunction(id) {
            location.href = "<s:url value="/order/unpaidMainSubmit?id="/>" + id;
        }

        /**
         * submitBtnClickFunction之前的提交逻辑，暂时这里备用
         */
        function originalSubmitBtnAction() {
            var grid = $("#declarationsDataGrid");
            var id = grid.gridMethods().getId();
            if (id) {
                //TODO: 添加弹窗询问支付
                var idsArr = grid.gridMethods().getIds();
                if (idsArr.length <= 5) {
                    jw.confirm("是否立即提交", function () {
                        var params = idsArr.join(",").replace(/,/gi, '&declaration_ids=');
                        window.location.href =
                                "../order/payPage?id=" + params;
                    });
                }
                else {
                    jw.alert("批量提交功能目前暂时只能一次提交不超过5票单。")
                }
            } else {
                jw.alert("请点击需要操作的数据!");
            }
        }

    </script>
</head>
<body>
<div id="declarationsDataTable"></div>
</body>
</html>
